﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FullThrustLogic.ShipComponent;

namespace FullThrustLogic.Core.GameState
{
    public class MovementState : iGameState
    {
        private GameContext _gameContext;

        public event GameContext.GameStateChangedHandler OnGameStateChanged;

        private bool ReadyToAdvance
        {
            get
            {
                foreach(Player player in _gameContext.Players)
                {
                    if(!player.MovementOrdersCommited)
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        public void AdvanceState()
        {
            ExecuteMovementOrders();

            if (OnGameStateChanged != null)
            {
                OnGameStateChanged(this, new EventArgs());
            }
        }

        public MovementState(GameContext gameContext)
        {
            _gameContext = gameContext;

            foreach(Player player in _gameContext.Players)
            {
                player.MovementOrdersCommited = false;

            }
        }

        public void ExecuteMovementOrders()
        {
            if (ReadyToAdvance)
            {
                foreach (Player player in _gameContext.Players)
                {
                    foreach (Ship ship in player.Ships)
                    {
                        ship.ExecuteMovementOrder(true);
                    }
                }
            }
        }
    }
}
